package com.chenyongjing.design.pattern.observer.general;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author chen
 * @Date 2022/12/5 21:03
 */
public class ConcreteSubject<E> implements ISubject<E>{
    private List<IObserver<E>> mObserver = new ArrayList<IObserver<E>>();

    @Override
    public boolean attach(IObserver<E> observer) {
        return !this.mObserver.contains(observer) && this.mObserver.add(observer);
    }

    @Override
    public boolean detach(IObserver<E> observer) {
        return this.mObserver.remove(observer);
    }

    @Override
    public void notify(E event) {
        for (IObserver<E> observer:this.mObserver){
            observer.update(event);
        }
    }
}
